package com.sakura.probe.controller.admin;

import com.sakura.probe.common.api.CommonResult;
import com.sakura.probe.common.api.Page;
import com.sakura.probe.common.util.HttpServletRequestUtil;
import com.sakura.probe.pojo.FeedBack;
import com.sakura.probe.pojo.PersonInfo;
import com.sakura.probe.service.FeedBackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.plugin.com.event.COMEventHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "AdminFeedBackController",description = "留言管理的相关操作")
@Controller
@Slf4j
@RequestMapping("/backstage/feedBack")
public class AdminFeedBackController {
    @Autowired
    private FeedBackService feedBackService;

    @ApiOperation("根据搜索条件分页显示反馈内容列表")
    @GetMapping("/listByCondition")
    @ResponseBody
    @RequiresRoles(logical = Logical.OR,value = {"1","2","3"})
    public CommonResult<Page<FeedBack>> getListByCondition(String name,String content,int pageIndex,int pageSize){
        FeedBack condition = conbination(name,content);
        Page<FeedBack> page = feedBackService.getListByCondition(condition, pageIndex, pageSize);
        return CommonResult.success(page);
    }

    @ApiOperation("删除单个留言")
    @GetMapping("/del")
    @ResponseBody
    public CommonResult delFeedBack(Long id){
        boolean result = feedBackService.removeById(id);
        return result?CommonResult.success("删除成功"):CommonResult.failed("删除失败");
    }

    @ApiOperation("删除多个留言")
    @GetMapping("/delAll")
    @ResponseBody
    public CommonResult delBatchFeedBack(HttpServletRequest request){
        List<Long> list = HttpServletRequestUtil.getLongList(request, "id", 10);
        boolean result = feedBackService.removeByIds(list);
        return result?CommonResult.success("删除成功"):CommonResult.failed("删除失败");
    }

    private FeedBack conbination(String name, String content) {
        FeedBack result = new FeedBack();
        if (null!=name && !"".equals(name)){
            PersonInfo person = new PersonInfo();
            person.setUsername(name);
            result.setPerson(person);
        }
        if (null!=content && !"".equals(content)){
            result.setSuggestion(content);
        }
        return result;
    }
}
